﻿using Caliburn.Micro;
using myapp.Events.System;
using myapp.Events.ViewModel;
using myapp.Instances;
using myappcore.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp.ViewModels
{
    /// <summary>
    /// Function Active View Model
    /// </summary>
    public class FunctionActiveViewModel : IBaseContentViewModel,
        IHandle<ViewModelFunctionSelectedEvent>,
        IHandle<ViewModelChangedEvent>,
        IHandle<ViewModelFunctionActiveShowEvent>
    {
        #region Attribute

        /// <summary>
        /// Current content of Active ViewModel
        /// </summary>
        private Screen _activeViewModel;

        /// <summary>
        /// Flag to show Home ViewModel
        /// </summary>
        private bool _isShowFunctionViewModel;

        /// <summary>
        /// Home ViewModel
        /// </summary>
        private Screen _functionViewModel;

        /// <summary>
        /// Logger
        /// </summary>
        private readonly ILog Logger = LogManager.GetLog(typeof(FunctionActiveViewModel));

        #endregion

        #region Properties

        /// <summary>
        /// Active View Model
        /// </summary>
        public Screen ActiveViewModel
        {
            get
            {
                return _activeViewModel;
            }
            private set
            {
                if (null != value)
                {
                    _activeViewModel = value;
                    NotifyOfPropertyChange(() => ActiveViewModel);
                }
                else
                {
                    Logger.Info("You have set null ActiveViewModel");
                }
            }

        }

        /// <summary>
        /// List of function view
        /// </summary>
        public Screen FunctionManagerViewModel
        {
            get
            {
                if (_isShowFunctionViewModel)
                {
                    return _functionViewModel;
                }
                return null;
            }
            private set
            {
                if (null != value)
                {
                    _functionViewModel = value;
                    NotifyOfPropertyChange(() => FunctionManagerViewModel);
                }
                else
                {
                    Logger.Info("You have set null FunctionManagerViewModel");
                }
            }
        }
        #endregion

        #region Constructor
        public FunctionActiveViewModel()
        {
            Logger.Info("Create " + this);
            DisplayName = "Xin chọn chức năng";
            CurrentColor = MainInstance.Instance.ColorConfig.FunctionViewBarActive;
            _functionViewModel = new FunctionManagerViewModel();
            MainInstance.Instance.EventAggregator.Subscribe(this);
        }
        #endregion

        #region Method
        /// <summary>
        /// Process function selected
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelFunctionSelectedEvent message)
        {
            if (null != message && null != message.FunctionViewModel)
            {
                DisplayName = message.FunctionViewModel.Function.Name;
                ActiveViewModel = message.FunctionViewModel.ActionViewModel;
                NotifyOfPropertyChange(() => DisplayName);
                NotifyOfPropertyChange(() => ActiveViewModel);

                MainInstance.Instance.EventAggregator.Publish(new ViewModelFunctionActiveShowEvent() { IsShow = true });
            }
            else
            {

            }
        }

        /// <summary>
        /// Changed Content Function View
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelChangedEvent message)
        {
            if (null != message && null != message.ViewModelChanged)
            {
                ActiveViewModel = message.ViewModelChanged;
                NotifyOfPropertyChange(() => ActiveViewModel);
            }
        }

        /// <summary>
        /// Home selected
        /// </summary>
        public void HomeSelected()
        {
            _isShowFunctionViewModel = !_isShowFunctionViewModel;
            NotifyOfPropertyChange(() => FunctionManagerViewModel);

        }

        /// <summary>
        /// Exit select
        /// </summary>
        public void ExitSelected()
        {
            _isShowFunctionViewModel = !_isShowFunctionViewModel;
            MainInstance.Instance.EventAggregator.Publish(new CloseApplicationEvent());
        }

        /// <summary>
        /// Changed Content Function View
        /// </summary>
        /// <param name="message"></param>
        public void Handle(ViewModelFunctionActiveShowEvent message)
        {
            if (null != message && message.IsShow != _isShowFunctionViewModel)
            {
                _isShowFunctionViewModel = message.IsShow;
                NotifyOfPropertyChange(() => FunctionManagerViewModel);
            }
        }
        #endregion
    }
}
